package com.bootdo.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.front.vo.Result;
import com.bootdo.system.vo.CareerfairPageResultVo;
import com.bootdo.system.vo.QueryCareerfairPageVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.CareerfairDO;
import com.bootdo.system.service.CareerfairService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 招聘会表
 * 
 * @author wudl
 * @email 460030103@qq.com
 * @date 2020-02-21 15:03:59
 */
 
@Controller
@RequestMapping("/system/careerfair")
public class CareerfairController {
	@Autowired
	private CareerfairService careerfairService;
	
	@GetMapping()
	@RequiresPermissions("system:careerfair:careerfair")
	String Careerfair(){
	    return "system/careerfair/careerfair";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:careerfair:careerfair")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CareerfairDO> careerfairList = careerfairService.list(query);
		int total = careerfairService.count(query);
		PageUtils pageUtils = new PageUtils(careerfairList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:careerfair:add")
	String add(){
	    return "system/careerfair/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:careerfair:edit")
	String edit(@PathVariable("id") Long id,Model model){
		CareerfairDO careerfair = careerfairService.get(id);
		model.addAttribute("careerfair", careerfair);
	    return "system/careerfair/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:careerfair:add")
	public R save( CareerfairDO careerfair){
		if(careerfairService.save(careerfair)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:careerfair:edit")
	public R update( CareerfairDO careerfair){
		careerfairService.update(careerfair);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:careerfair:remove")
	public R remove( Long id){
		if(careerfairService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:careerfair:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		careerfairService.batchRemove(ids);
		return R.ok();
	}

	@ResponseBody
	@PostMapping("/query")
	public Result query(@RequestBody QueryCareerfairPageVo page){
		if(page.getPageNumber() == null){
			page.setPageNumber(1);
		}
		if(page.getPageSize() == null){
			page.setPageSize(10);
		}

		Map<String, Object> params =  new HashMap<>();
		params.put("offset",(page.getPageNumber() -1)*page.getPageSize());
		params.put("limit",page.getPageSize());

		//查询列表数据
		Query query = new Query(params);
		List<CareerfairDO> careerfairList = careerfairService.list(query);
		int total = careerfairService.count(query);

		CareerfairPageResultVo result = new  CareerfairPageResultVo();
		result.setTotal(total);
		result.setList(careerfairList);

		return Result.ok(result);
	}

	@ResponseBody
	@GetMapping("/getById/{id}")
	public Result getById(@PathVariable("id") Long id) {
		CareerfairDO careerfair = careerfairService.get(id);
		return Result.ok(careerfair);
	}
}
